<?php

namespace App\Http\ManagerApi\Controllers;

use App\Api\BaseClass\Controller;
use Illuminate\Support\Facades\Crypt;
use App\Http\ManagerApi\Models\PlatformSetting;
use App\Http\ManagerApi\Models\PlatformRangeSetting;
use Illuminate\Support\Facades\DB;
use App\Http\ManagerApi\Models\UserIntegralLog;
use App\Http\ManagerApi\Models\TeamMember;
use App\Http\ManagerApi\Models\CashRequestLog;
use App\Http\WechatAuth\Models\UserOrder;

class ParamsController extends Controller
{
    public function setParams()
    {
        $rules = [
            // 平台注册手续费比例
            ['service_charge_rate', 'required|numeric'],
            // 佣金比例
            ['commission_rate', 'required|numeric'],
            // 直属下级获取上级返利最小推荐人数
            ['sub_max_number', 'required|integer'],
            // 直属下级获取上级返利比例
            ['sub_rebate_rate', 'required|numeric'],
            // 激活密码
            ['activation_cipher', 'string'],
            // 直推分红比例
            ['direct_rate', 'required'],
            // 股东(运营中心)分红比例
            ['partner_rate', 'required'],
            // 股东分红比例
            ['shareholder_rate', 'required'],
        ];

        $params = $this->form->camelFormOrFail($rules);

        if (isset($params['activation_cipher']) && !empty($params['activation_cipher'])) {
            $params['activation_cipher'] = Crypt::encryptString($params['activation_cipher']);
        }

        $data = PlatformSetting::first();
        if (!isset($data)) {
            $data = new PlatformSetting();
        }

        return $this->form->updateMessage($data->fill($params)->save());
    }

    public function getParams()
    {
        return $this->form->datas(PlatformSetting::first());
    }

    /**
     * 级差设置
     * @author yff
     * @date 2018-9-19 11:44:13
     */
    public function getRangeSetting()
    {
        return $this->form->datas(PlatformRangeSetting::orderBy('condition')->get());
    }

    /**
     * 添加级差设置
     * @param int $condition 条件
     * @param num $rate 比例
     * @author yff
     * @date 2018-9-19 11:46:18
     */
    public function addRangeSetting()
    {
        $rules = [
            ['level', 'required|string'],
            ['condition', 'required|integer'],
            ['rate', 'required|numeric'],
        ];

        $params = $this->form->camelFormOrFail($rules);

        // 查看是否有相同的设置
        $set = PlatformRangeSetting::where('condition', $params['condition'])->first();
        if (isset($set)) {
            return $this->form->error('添加失败，该金额已设置比例');
        }

        return $this->form->createMessage(PlatformRangeSetting::create($params));
    }

    /**
     * 修改级差设置
     * @param int id
     * @author yff
     * @date 2018-9-19 11:46:04
     */
    public function updateRangeSetting()
    {
        $rules = [
            ['id', 'required|integer'],
            ['level', 'string'],
            ['condition', 'integer'],
            ['rate', 'numeric'],
        ];

        $params = $this->form->camelFormOrFail($rules);

        $set = PlatformRangeSetting::findOrFail($params['id']);

        if ($set->condition != $params['condition'] && PlatformRangeSetting::where('condition', $params['condition'])->exists()) {
            return $this->form->error('修改失败，该金额已设置比例');
        }

        return $this->form->updateMessage($set->fill($params)->save());
    }

    /**
     * 删除级差设置
     * @param int id
     * @author yff
     * @date2018-9-19 11:49:15
     */
    public function deleteRangeSetting()
    {
        $rules = [
            ['id', 'required|integer'],
        ];

        $params = $this->form->camelFormOrFail($rules);

        return $this->form->deleteMessage(PlatformRangeSetting::destroy($params['id']));
    }

    /**
     * 清空数据
     */
    public function truncate()
    {
        // 清空积分记录表、团队表、提现记录表、订单表
        DB::transaction(function () {
            UserIntegralLog::truncate();
            TeamMember::truncate();
            CashRequestLog::truncate();
            UserOrder::truncate();
        });

        return $this->form->success('清除成功');
    }
}